// 引入 gulp
var gulp = require('gulp'); 

// 引入组件
var less = require('gulp-less');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var rename = require('gulp-rename');
var imagemin = require('gulp-imagemin');
var minifyHtml= require("gulp-minify-html");
var minifyCSS = require('gulp-minify-css');
var gulpif = require('gulp-if');
// html include
var contentInclude = require('gulp-content-includer');

// var condition = function(f){
//     if(f.path.endswith('.min.js')){
//         return false;
//     }
//     return true;
// };

// gulp.task('scripts', function (cb) {
//     return gulp.src('src/js/*.js')
//         .pipe(gulpif(condition, uglify({mangle: {except: ['require' ,'exports' ,'module' ,'$']}}))) //排除混淆关键字
//         .pipe(gulp.dest('./dist/js'));

// });
// 合并，压缩js文件
gulp.task('scripts', function() {
    gulp.src('src/js/*.js')
        .pipe(concat('index.js'))
        .pipe(gulp.dest('./dist/js'))
        .pipe(rename('index.min.js'))
        .pipe(uglify())
        .pipe(gulp.dest('./dist/js'));
});
// 压缩图片
gulp.task('imagemin',function() {
    return gulp.src('src/images/*.*')
       .pipe(imagemin())
       .pipe(gulp.dest('./dist/images'));
});
// 编译less
gulp.task('less', function() {
    gulp.src('src/less/*.less')
        .pipe(less())
        .pipe(gulp.dest('src/css'));
});
// 合并，压缩css文件
gulp.task('css', function() {
    gulp.src('src/css/*.css')
        .pipe(concat('index.css'))
        .pipe(gulp.dest('./dist/css'))
        .pipe(rename('index.min.css'))
        .pipe(minifyCSS())
        .pipe(gulp.dest('./dist/css'));
});
// 引入 html
gulp.task('concathtml',function() {
    gulp.src("src/*.html")
        .pipe(contentInclude({
            includerReg:/<!\-\-include\s+"([^"]+)"\-\->/g,
            deepConcat: true,
            baseSrc: 'src/'
        }))
        .pipe(gulp.dest('src/html'));
});
// 压缩html
gulp.task('minify-html',function() {
     return gulp.src('src/html/*.html')//要压缩的html文件
        .pipe(minifyHtml())//压缩
        .pipe(gulp.dest('./dist'));
});
// 转移静态文件夹
gulp.task('move1',function() {
     return gulp.src('src/static/**/*').pipe(gulp.dest('./dist/static'));
});
gulp.task('move2',function() {
     return gulp.src('src/fonts/**/*').pipe(gulp.dest('./dist/fonts'));
});

// 默认任务
gulp.task('default', function(){
    gulp.run('less','scripts','imagemin','css','concathtml','minify-html','move1','move2');

    // 监听文件变化
    gulp.watch('src/less/*.less', function(){
        gulp.run('less');
    });
    gulp.watch('src/js/*.js', function(){
        gulp.run('scripts');
    });
    gulp.watch('src/images/*.*', function(){
        gulp.run('imagemin');
    });
    gulp.watch('src/css/*.css', function(){
        gulp.run('css');
    });
    gulp.watch('src/*.html', function(){
        gulp.run('concathtml');
    });
     gulp.watch('src/html/*.html', function(){
        gulp.run('minify-html');
    });
      gulp.watch('src/src/*.html', function(){
        gulp.run('concathtml');
    });
    gulp.watch('src/src/*.html', function(){
        gulp.run('minify-html');
    });
});